home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / gas_251.zip / bin_251 / bfd / aout-ns32k.c < prev    next >
C/C++ Source or Header  |  1994-08-08  |  13KB  |  399 lines

  1. /* BFD back-end for ns32k a.out-ish binaries.
  2.    Copyright (C) 1990, 1991, 1992, 1994 Free Software Foundation, Inc.
  3.    Contributed by Ian Dall (idall@eleceng.adelaide.edu.au).
  4.  
  5. This file is part of BFD, the Binary File Descriptor library.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. #define BYTES_IN_WORD 4
  22.  
  23. #include "bfd.h"
  24. #include "aout/aout64.h"
  25.  
  26. #define MYNS(OP) CAT(ns32kaout_,OP)
  27. CONST struct reloc_howto_struct *
  28. MYNS(bfd_reloc_type_lookup)
  29.   PARAMS((bfd *abfd AND
  30.       bfd_reloc_code_real_type code));
  31.  
  32. boolean
  33. MYNS(write_object_contents)
  34.   PARAMS((bfd *abfd));
  35.  
  36. /* Avoid multiple definitions from aoutx if supporting standard a.out format(s)
  37.  * as well as this one
  38.  */
  39. #define NAME(x,y) CAT3(ns32kaout,_32_,y)
  40.  
  41. void bfd_ns32k_arch PARAMS ((void));
  42. long ns32k_get_displacement PARAMS ((bfd_byte *buffer, long offset, long size));
  43. int ns32k_put_displacement PARAMS ((long value, bfd_byte *buffer, long offset, long size));
  44. long ns32k_get_immediate PARAMS ((bfd_byte *buffer, long offset, long size));
  45. int ns32k_put_immediate  PARAMS ((long value, bfd_byte *buffer, long offset, long size));
  46. bfd_reloc_status_type
  47.   ns32k_reloc_disp PARAMS ((bfd *abfd, arelent *reloc_entry,
  48.            struct symbol_cache_entry *symbol,
  49.            PTR data,
  50.            asection *input_section,
  51.            bfd *output_bfd,
  52.            char **error_message));
  53. bfd_reloc_status_type
  54.   ns32k_reloc_imm  PARAMS ((bfd *abfd,
  55.            arelent *reloc_entry,
  56.            struct symbol_cache_entry *symbol,
  57.            PTR data,
  58.            asection *input_section,
  59.            bfd *output_bfd,
  60.            char **error_message));
  61. bfd_reloc_status_type
  62.   ns32k_final_link_relocate  PARAMS (( const reloc_howto_type *howto,
  63.                  bfd *input_bfd,
  64.                  asection *input_section,
  65.                  bfd_byte *contents,
  66.                  bfd_vma address,
  67.                  bfd_vma value,
  68.                  bfd_vma addend ));
  69. bfd_reloc_status_type
  70.   ns32k_relocate_contents  PARAMS ((const reloc_howto_type *howto,
  71.                bfd *input_bfd,
  72.                bfd_vma relocation,
  73.                bfd_byte *location));
  74.  
  75. #include "libaout.h"
  76.  
  77. #define MY(OP) MYNS(OP)
  78.  
  79. #define MY_swap_std_reloc_in MY(swap_std_reloc_in)
  80. #define MY_swap_std_reloc_out MY(swap_std_reloc_out)
  81.  
  82. static void
  83. MY_swap_std_reloc_in PARAMS ((bfd *abfd, struct reloc_std_external *bytes,
  84.                   arelent *cache_ptr, asymbol **symbols));
  85.  
  86. static void
  87. MY_swap_std_reloc_out PARAMS ((bfd *abfd, arelent *g,
  88.                    struct reloc_std_external *natptr));
  89.  
  90. /* The ns32k series is ah, unusual, when it comes to relocation.
  91.  * There are three storage methods for relocateable objects.  There
  92.  * are displacements, immediate operands and ordinary twos complement
  93.  * data. Of these, only the last fits into the standard relocation
  94.  * scheme.  Immediate operands are stored huffman encoded and
  95.  * immediate operands are stored big endian (where as the natural byte
  96.  * order is little endian for this achitecture).
  97.  
  98.  * Note that the ns32k displacement storage method is orthogonal to
  99.  * whether the relocation is pc relative or not. The "displacement"
  100.  * storage scheme is used for essentially all address constants. The
  101.  * displacement can be relative to zero (absolute displacement),
  102.  * relative to the pc (pc relative), the stack pointer, the frame
  103.  * pointer, the static base register and general purpose register etc.
  104.  
  105.  * For example:
  106.  *
  107.  *  sym1: .long .    # pc relative 2's complement
  108.  *  sym1: .long foo    # 2's complement not pc relative
  109.  *
  110.  *  self:  movd @self, r0 # pc relative displacement
  111.  *       movd foo, r0 # non pc relative displacement
  112.  *
  113.  *  self:  movd self, r0 # pc relative immediate
  114.  *         movd foo, r0 # non pc relative immediate
  115.  *
  116.  * In addition, for historical reasons the encoding of the relocation types
  117.  * in the a.out format relocation entries is such that even the relocation
  118.  * methods which are standard are not encoded the standard way.
  119.  *
  120.  */
  121.  
  122. reloc_howto_type MY(howto_table)[] = 
  123. {
  124. /* ns32k immediate operands */
  125. HOWTO(BFD_RELOC_NS32K_IMM_8, 0, 0, 8, false, 0, true,
  126.        ns32k_reloc_imm, "NS32K_IMM_8",
  127.        true, 0x000000ff,0x000000ff, false),
  128. HOWTO(BFD_RELOC_NS32K_IMM_16, 0, 1, 16, false, 0, true,
  129.        ns32k_reloc_imm,  "NS32K_IMM_16",
  130.        true, 0x0000ffff,0x0000ffff, false),
  131. HOWTO(BFD_RELOC_NS32K_IMM_32, 0, 2, 32, false, 0, true,
  132.        ns32k_reloc_imm, "NS32K_IMM_32",
  133.        true, 0xffffffff,0xffffffff, false),
  134. HOWTO(BFD_RELOC_NS32K_IMM_8_PCREL, 0, 0, 8, true, 0, false,
  135.        ns32k_reloc_imm, "PCREL_NS32K_IMM_8",
  136.        true, 0x000000ff, 0x000000ff, false),
  137. HOWTO(BFD_RELOC_NS32K_IMM_16_PCREL, 0, 1, 16, true, 0, false,
  138.        ns32k_reloc_imm, "PCREL_NS32K_IMM_16",
  139.        true, 0x0000ffff,0x0000ffff, false),
  140. HOWTO(BFD_RELOC_NS32K_IMM_32_PCREL, 0, 2, 32, true, 0, false,
  141.        ns32k_reloc_imm, "PCREL_NS32K_IMM_32",
  142.        true, 0xffffffff,0xffffffff, false),
  143.  
  144. /* ns32k displacements */
  145. HOWTO(BFD_RELOC_NS32K_DISP_8, 0, 0, 8, false, 0, true,
  146.        ns32k_reloc_disp, "NS32K_DISP_8",
  147.        true, 0x000000ff,0x000000ff, false),
  148. HOWTO(BFD_RELOC_NS32K_DISP_16, 0, 1, 16, false, 0, true,
  149.        ns32k_reloc_disp, "NS32K_DISP_16",
  150.        true, 0x0000ffff, 0x0000ffff, false),
  151. HOWTO(BFD_RELOC_NS32K_DISP_32, 0, 2, 32, false, 0, true,
  152.        ns32k_reloc_disp, "NS32K_DISP_32",
  153.        true, 0xffffffff, 0xffffffff, false),
  154. HOWTO(BFD_RELOC_NS32K_DISP_8_PCREL, 0, 0, 8, true, 0, false,
  155.        ns32k_reloc_disp, "PCREL_NS32K_DISP_8",
  156.        true, 0x000000ff,0x000000ff, false),
  157. HOWTO(BFD_RELOC_NS32K_DISP_16_PCREL, 0, 1, 16, true, 0, false,
  158.        ns32k_reloc_disp, "PCREL_NS32K_DISP_16",
  159.        true, 0x0000ffff,0x0000ffff, false),
  160. HOWTO(BFD_RELOC_NS32K_DISP_32_PCREL, 0, 2, 32, true, 0, false,
  161.        ns32k_reloc_disp, "PCREL_NS32K_DISP_32",
  162.        true, 0xffffffff,0xffffffff, false),
  163.  
  164. /* Normal 2's complement */
  165. HOWTO(BFD_RELOC_8, 0, 0, 8, false, 0, complain_overflow_bitfield,0,
  166.       "8", true, 0x000000ff,0x000000ff, false),
  167. HOWTO(BFD_RELOC_16, 0, 1, 16, false, 0, complain_overflow_bitfield,0,
  168.       "16", true, 0x0000ffff,0x0000ffff, false),
  169. HOWTO(BFD_RELOC_32, 0, 2, 32, false, 0, complain_overflow_bitfield,0,
  170.       "32", true, 0xffffffff,0xffffffff, false),
  171. HOWTO(BFD_RELOC_8_PCREL, 0, 0, 8, true, 0, complain_overflow_signed, 0,
  172.       "PCREL_8", true, 0x000000ff,0x000000ff, false),
  173. HOWTO(BFD_RELOC_16_PCREL, 0, 1, 16, true, 0, complain_overflow_signed, 0,
  174.       "PCREL_16", true, 0x0000ffff,0x0000ffff, false),
  175. HOWTO(BFD_RELOC_32_PCREL, 0, 2, 32, true, 0, complain_overflow_signed, 0,
  176.       "PCREL_32", true, 0xffffffff,0xffffffff, false),
  177. };
  178.  
  179. #define RELOC_STD_BITS_NS32K_TYPE_BIG 0x06
  180. #define RELOC_STD_BITS_NS32K_TYPE_LITTLE 0x60
  181. #define RELOC_STD_BITS_NS32K_TYPE_SH_BIG 1
  182. #define RELOC_STD_BITS_NS32K_TYPE_SH_LITTLE 5
  183.  
  184. reloc_howto_type *
  185. MY(reloc_howto)(abfd, rel, r_index, r_extern, r_pcrel)
  186.      bfd *abfd;
  187.      struct reloc_std_external *rel;
  188.      int *r_index;
  189.      int *r_extern;
  190.      int *r_pcrel;
  191. {
  192.   unsigned int r_length;
  193.   int r_ns32k_type;
  194. /*  BFD_ASSERT(abfd->xvec->header_byteorder_big_p == false); */
  195.   *r_index =  ((rel->r_index[2] << 16)
  196.            | (rel->r_index[1] << 8)
  197.            |  rel->r_index[0] );
  198.   *r_extern  = (0 != (rel->r_type[0] & RELOC_STD_BITS_EXTERN_LITTLE));
  199.   *r_pcrel   = (0 != (rel->r_type[0] & RELOC_STD_BITS_PCREL_LITTLE));
  200.   r_length  =  ((rel->r_type[0] & RELOC_STD_BITS_LENGTH_LITTLE)
  201.         >> RELOC_STD_BITS_LENGTH_SH_LITTLE);
  202.   r_ns32k_type  =  ((rel->r_type[0] & RELOC_STD_BITS_NS32K_TYPE_LITTLE)
  203.             >> RELOC_STD_BITS_NS32K_TYPE_SH_LITTLE);
  204.   return (MY(howto_table) + r_length + 3 * (*r_pcrel) + 6 * r_ns32k_type);
  205. }
  206.  
  207. #define MY_reloc_howto(BFD,REL,IN,EX,PC) MY(reloc_howto)(BFD, REL, &IN, &EX, &PC)
  208.  
  209. void
  210. MY(put_reloc)(abfd, r_extern, r_index, value, howto, reloc)
  211.      bfd *abfd;
  212.      int r_extern;
  213.      int r_index;
  214.      long value;
  215.      const reloc_howto_type *howto;
  216.      struct reloc_std_external *reloc;
  217. {
  218.   unsigned int r_length;
  219.   int r_pcrel;
  220.   int r_ns32k_type;
  221.   PUT_WORD (abfd, value, reloc->r_address);
  222.   r_length = howto->size ;    /* Size as a power of two */
  223.   r_pcrel  = (int) howto->pc_relative; /* Relative to PC? */
  224.   r_ns32k_type = (howto - MY(howto_table) )/6;
  225. /*  BFD_ASSERT (abfd->xvec->header_byteorder_big_p == false); */
  226.   reloc->r_index[2] = r_index >> 16;
  227.   reloc->r_index[1] = r_index >> 8;
  228.   reloc->r_index[0] = r_index;
  229.   reloc->r_type[0] =
  230.     (r_extern?    RELOC_STD_BITS_EXTERN_LITTLE: 0)
  231.       | (r_pcrel?     RELOC_STD_BITS_PCREL_LITTLE: 0)
  232.     | (r_length <<  RELOC_STD_BITS_LENGTH_SH_LITTLE)
  233.       | (r_ns32k_type <<  RELOC_STD_BITS_NS32K_TYPE_SH_LITTLE);
  234. }
  235.  
  236. #define MY_put_reloc(BFD, EXT, IDX, VAL, HOWTO, RELOC) \
  237.   MY(put_reloc)(BFD, EXT, IDX, VAL, HOWTO, RELOC)
  238.  
  239. #define STAT_FOR_EXEC
  240.  
  241. #define MY_final_link_relocate ns32k_final_link_relocate
  242. #define MY_relocate_contents ns32k_relocate_contents
  243.  
  244. #include <aoutx.h>
  245.  
  246. CONST struct reloc_howto_struct *
  247.   MY(bfd_reloc_type_lookup)(abfd,code)
  248.       bfd *abfd;
  249.       bfd_reloc_code_real_type code;
  250. {
  251.  
  252. #define ENTRY(i,j)    case i: return &MY(howto_table)[j]
  253.  
  254.   int ext = obj_reloc_entry_size (abfd) == RELOC_EXT_SIZE;
  255.  
  256.   BFD_ASSERT(ext == 0);
  257.   if (code == BFD_RELOC_CTOR)
  258.     switch (bfd_get_arch_info (abfd)->bits_per_address)
  259.       {
  260.       case 32:
  261.     code = BFD_RELOC_32;
  262.     break;
  263.       }
  264.   switch (code)
  265.     {
  266.       ENTRY(BFD_RELOC_NS32K_IMM_8, 0);
  267.       ENTRY(BFD_RELOC_NS32K_IMM_16, 1);
  268.       ENTRY(BFD_RELOC_NS32K_IMM_32, 2);
  269.       ENTRY(BFD_RELOC_NS32K_IMM_8_PCREL, 3);
  270.       ENTRY(BFD_RELOC_NS32K_IMM_16_PCREL, 4);
  271.       ENTRY(BFD_RELOC_NS32K_IMM_32_PCREL, 5);
  272.       ENTRY(BFD_RELOC_NS32K_DISP_8, 6);
  273.       ENTRY(BFD_RELOC_NS32K_DISP_16, 7);
  274.       ENTRY(BFD_RELOC_NS32K_DISP_32, 8);
  275.       ENTRY(BFD_RELOC_NS32K_DISP_8_PCREL, 9);
  276.       ENTRY(BFD_RELOC_NS32K_DISP_16_PCREL, 10);
  277.       ENTRY(BFD_RELOC_NS32K_DISP_32_PCREL, 11);
  278.       ENTRY(BFD_RELOC_8, 12);
  279.       ENTRY(BFD_RELOC_16, 13);
  280.       ENTRY(BFD_RELOC_32, 14);
  281.       ENTRY(BFD_RELOC_8_PCREL, 15);
  282.       ENTRY(BFD_RELOC_16_PCREL, 16);
  283.       ENTRY(BFD_RELOC_32_PCREL, 17);
  284.     default: return (CONST struct reloc_howto_struct *) 0;
  285.     }
  286. #undef ENTRY
  287. }
  288.  
  289.  
  290. static void
  291. MY_swap_std_reloc_in (abfd, bytes, cache_ptr, symbols)
  292.      bfd *abfd;
  293.      struct reloc_std_external *bytes;
  294.      arelent *cache_ptr;
  295.      asymbol **symbols;
  296. {
  297.   int r_index;
  298.   int r_extern;
  299.   unsigned int r_length;
  300.   int r_pcrel;
  301.   int r_ns32k_type;
  302.   struct aoutdata  *su = &(abfd->tdata.aout_data->a);
  303.  
  304.   cache_ptr->address = bfd_h_get_32 (abfd, bytes->r_address);
  305.  
  306.   /* now the fun stuff */
  307.  
  308.   cache_ptr->howto = MY_reloc_howto(abfd, bytes, r_index, r_extern, r_pcrel);
  309.  
  310.   MOVE_ADDRESS(0);
  311. }
  312.  
  313. static void
  314. MY_swap_std_reloc_out (abfd, g, natptr)
  315.      bfd *abfd;
  316.      arelent *g;
  317.      struct reloc_std_external *natptr;
  318. {
  319.   int r_index;
  320.   asymbol *sym = *(g->sym_ptr_ptr);
  321.   int r_extern;
  322.   unsigned int r_length;
  323.   int r_pcrel;
  324.   int r_ns32k_type;
  325.   unsigned int r_addend;
  326.   asection *output_section = sym->section->output_section;
  327.  
  328.   r_addend = g->addend + (*(g->sym_ptr_ptr))->section->output_section->vma;
  329.     
  330.   /* name was clobbered by aout_write_syms to be symbol index */
  331.  
  332.   /* If this relocation is relative to a symbol then set the 
  333.      r_index to the symbols index, and the r_extern bit.
  334.  
  335.      Absolute symbols can come in in two ways, either as an offset
  336.      from the abs section, or as a symbol which has an abs value.
  337.      Check for that here.  */
  338.  
  339.   if (bfd_is_com_section (output_section)
  340.       || output_section == &bfd_abs_section
  341.       || output_section == &bfd_und_section) 
  342.     {
  343.       if (bfd_abs_section.symbol == sym)
  344.     {
  345.       /* Whoops, looked like an abs symbol, but is really an offset
  346.          from the abs section */
  347.       r_index = 0;
  348.       r_extern = 0;
  349.     }
  350.       else 
  351.     {
  352.       /* Fill in symbol */
  353.       r_extern = 1;
  354. #define KEEPIT flags
  355.       r_index =  stoi((*(g->sym_ptr_ptr))->KEEPIT);
  356. #undef KEEPIT     
  357.     }
  358.     }
  359.   else 
  360.     {
  361.       /* Just an ordinary section */
  362.       r_extern = 0;
  363.       r_index  = output_section->target_index;      
  364.     }
  365.  
  366.   MY_put_reloc (abfd, r_extern, r_index, g->address, g->howto, natptr);
  367. }
  368.  
  369. bfd_reloc_status_type
  370. ns32k_relocate_contents (howto, input_bfd, relocation, location)
  371.      const reloc_howto_type *howto;
  372.      bfd *input_bfd;
  373.      bfd_vma relocation;
  374.      bfd_byte *location;
  375. {
  376.   int r_ns32k_type = (howto - MY(howto_table)) / 6;
  377.   long (*get_data)();
  378.   int (*put_data)();
  379.  
  380.   switch (r_ns32k_type)
  381.     {
  382.     case 0:
  383.       get_data = ns32k_get_immediate;
  384.       put_data = ns32k_put_immediate;
  385.       break;
  386.     case 1:
  387.       get_data = ns32k_get_displacement;
  388.       put_data = ns32k_put_displacement;
  389.       break;
  390.     case 2:
  391.       return _bfd_relocate_contents (howto, input_bfd, relocation,
  392.                     location);
  393.       /* NOT REACHED */
  394.       break;
  395.     }
  396.   return do_ns32k_reloc_contents (howto, input_bfd, relocation,
  397.                  location, get_data, put_data);
  398. }
  399.